package com.wanmait.zaisousuo.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.zaisousuo.mapper.QuestionMapper;
import com.wanmait.zaisousuo.mapper.UserinfoMapper;
import com.wanmait.zaisousuo.pojo.Question;
import com.wanmait.zaisousuo.pojo.Userinfo;
import com.wanmait.zaisousuo.service.AnswerService;
import com.wanmait.zaisousuo.util.GetVisitTime;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import com.wanmait.zaisousuo.mapper.AnswerMapper;
import com.wanmait.zaisousuo.pojo.Answer;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AnswerServiceImpl implements AnswerService {

    @Resource
    private AnswerMapper answerMapper;
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private UserinfoMapper userinfoMapper;

//    前台添加
    @Override
    public void insert(Answer object) {

        String description=object.getDescription();
        if(description.length()>20) {
            description = description.substring(0, 19) + "...";
        }
        object.setDescription(description);

        answerMapper.insert(object);
        questionMapper.increaseCount(object.getQuestion().getId());
        userinfoMapper.answerCountIncrease(object.getUserinfo().getId());
    }


    @Override
    public void update(Answer object) {

    }

//    单个真删除
    @Override
    public void delete(Integer id) {
        answerMapper.deleteByPrimaryKey(id);
    }
//    多个回答禁用
    @Override
    public void delete(String ids) {
        String[] idsStr = ids.split(",");
        List<Integer> idList = new ArrayList<>();
        for (String id : idsStr) {
            idList.add(Integer.parseInt(id));

        }
        answerMapper.deleteAll(idList);
    }

//    通过回答id查找回答
    @Override
//    @Cacheable(value = "QuestionAnswerCache",key = "'findByAnswerId'+#id")
    public Answer findById(Integer id) {
        Answer answer = answerMapper.selectByPrimaryKey(id);
        return answer;
    }

    @Override
    public List<Answer> findAll() {
        return null;
    }

    @Override
    public List<Answer> findAll(Answer object) {
        return null;
    }

    @Override
    public int getDataCount() {
        return 0;
    }

    @Override
    public int getDataCount(Answer object) {
        return 0;
    }

    @Override
    public PageInfo<Answer> findList(int pageNum) {
        return null;
    }


//    通过问题分页查找
    @Override
//    @Cacheable(value = "QuestionAnswerCache",key = "'findAnswersByQuestionId'+#id+'pageNum'+#pageNum")
    public PageInfo<Answer> findByQuestionId(Integer questionId, Integer pageNum) {
        Integer pageSize=5;
        PageHelper.startPage(pageNum,pageSize);
        List<Answer> answers = answerMapper.findByQuestion(questionId);
        PageInfo<Answer> pageInfo = new PageInfo<>(answers,7);
        return pageInfo;
    }

//    回答采纳
    @Override
    public void answerAccepted(Answer answer) {
//        获取当前时间
        String visitTime = GetVisitTime.visitTime();
        Timestamp acceptTime = Timestamp.valueOf(visitTime);
        answer.setAcceptTime(acceptTime);
//      answer表中改变
        answerMapper.answerAccept(answer);
//        question表中完成
        questionMapper.finishedQuestion(answer.getQuestion().getId());
    }

//    通过问题来查找所有的答案
    @Override
    @Cacheable(value = "QuestionAnswerCache",key = "'findByQuestionAll'+#questionId")
    public List<Answer> findByQuestion(Integer questionId) {
        List<Answer> answers = answerMapper.findByQuestion(questionId);
        return answers;
    }
// 后台禁用以及恢复所选答案
    @Override
    public Integer updateDisabled(Integer id, boolean disabled) {
        answerMapper.updateDisabled(id,disabled);
        return id;
    }

//    通过用户id查找问题
    @Override
    @Cacheable(value = "QuestionAnswerCache",key = "'findAnswersByUserId'+#id+'pageNum'+#pageNum")
    public PageInfo<Answer> findByUserId(Integer id,Integer pageNum) {
        Integer pageSize=5;
        PageHelper.startPage(pageNum,pageSize);
        List<Answer> answers=answerMapper.findByUserId(id);
        PageInfo<Answer> pageInfo=new PageInfo<>(answers);
        return pageInfo;
    }

//    批量恢复所有答案
    @Override
    public void recover(String ids) {
        List<Integer> idList = new ArrayList<>();
        String[] idsStr = ids.split(",");
        for (String id : idsStr) {
            idList.add(Integer.parseInt(id));

        }
        answerMapper.recoverAll(idList);
    }
}
